<?php
if (!defined('ABSPATH')) {
    exit;
}

// 检查权限
global $wp_spider_monitor;
$is_premium = $wp_spider_monitor->is_premium_user();

// 获取当前配置的搜索引擎
$engines = $this->get_configured_engines();

// 搜索引擎配置信息
$engine_configs = array(
    'baidu' => array(
        'name' => '百度搜索',
        'description' => '百度公司旗下的搜索引擎',
        'token_url' => 'https://ziyuan.baidu.com/site/index',
        'help_text' => '在百度搜索资源平台获取推送接口的token',
        'placeholder' => '请输入百度推送token',
        'icon' => '🔍'
    ),
    'bing' => array(
        'name' => '必应搜索引擎',
        'description' => '微软旗下的搜索引擎',
        'token_url' => 'https://www.bing.com/webmasters/homepage',
        'help_text' => '在必应网站管理员工具中获取API密钥',
        'placeholder' => '请输入必应API密钥',
        'icon' => '🔷'
    ),
    'google' => array(
        'name' => '谷歌搜索引擎',
        'description' => '全球最大的搜索引擎',
        'token_url' => 'https://search.google.com/search-console',
        'help_text' => '在Google Search Console中获取服务账户密钥',
        'placeholder' => '请输入谷歌服务账户JSON',
        'icon' => '🔴'
    ),
    '360' => array(
        'name' => '360搜索',
        'description' => '360公司旗下的搜索引擎',
        'token_url' => 'https://zhanzhang.so.com/',
        'help_text' => '在360站长平台获取推送接口的token',
        'placeholder' => '请输入360推送token',
        'icon' => '🟢'
    ),
    'sogou' => array(
        'name' => '搜狗搜索',
        'description' => '搜狗公司旗下的搜索引擎',
        'token_url' => 'https://zhanzhang.sogou.com/',
        'help_text' => '在搜狗站长平台获取推送接口的token',
        'placeholder' => '请输入搜狗推送token',
        'icon' => '🟠'
    )
);

// 处理表单提交
if (isset($_POST['action']) && $_POST['action'] === 'save_engines_config') {
    check_admin_referer('save_engines_config_nonce');
    
    // 检查是否为付费功能
    if (!$is_premium) {
        echo '<div class="notice notice-error"><p>此功能需要激活许可证，请先激活许可证后使用</p></div>';
    } else {
        foreach ($engine_configs as $engine_type => $config) {
            $token = sanitize_text_field($_POST[$engine_type . '_token']);
            $is_active = isset($_POST[$engine_type . '_active']) ? 1 : 0;
            
            $this->update_engine_config($engine_type, array(
                'site_token' => $token,
                'is_active' => $is_active
            ));
        }
        
        echo '<div class="notice notice-success"><p>配置已保存！</p></div>';
    }
}

// 获取当前配置
$current_configs = array();
foreach ($engine_configs as $engine_type => $config) {
    $config_data = $this->get_engine_config($engine_type);
    $current_configs[$engine_type] = $config_data ? $config_data : array(
        'site_token' => '',
        'is_active' => 0
    );
}

?>

<div class="wrap">
    <h1>搜索引擎推送配置</h1>
    <p class="description">配置主流搜索引擎的推送接口，新文章发布时将自动推送到已启用的搜索引擎。</p>
    
    <?php if (!$is_premium): ?>
        <div class="premium-restriction">
            <span class="premium-icon">🔒</span>
            <span class="premium-text">搜索引擎推送功能需要激活许可证</span>
            <a href="<?php echo $wp_spider_monitor->get_license_page_url(); ?>" class="premium-link">立即激活</a>
        </div>
    <?php endif; ?>
    
    <form method="post" action="" <?php echo !$is_premium ? 'class="premium-disabled"' : ''; ?>>
        <?php wp_nonce_field('save_engines_config_nonce'); ?>
        <input type="hidden" name="action" value="save_engines_config">
        
        <div class="engines-grid">
            <?php foreach ($engine_configs as $engine_type => $config): 
                $current = $current_configs[$engine_type];
            ?>
            <div class="engine-card">
                <div class="engine-header">
                    <span class="engine-icon"><?php echo $config['icon']; ?></span>
                    <h3><?php echo $config['name']; ?></h3>
                </div>
                
                <div class="engine-body">
                    <p class="engine-description"><?php echo $config['description']; ?></p>
                    
                    <div class="form-field">
                        <label>
                            <input type="checkbox" 
                                   name="<?php echo $engine_type; ?>_active" 
                                   value="1" 
                                   <?php checked($current['is_active']); ?>>
                            启用推送
                        </label>
                    </div>
                    
                    <div class="form-field">
                        <label for="<?php echo $engine_type; ?>_token">
                            推送Token/API密钥：
                            <a href="<?php echo $config['token_url']; ?>" 
                               target="_blank" 
                               class="token-link" 
                               title="点击获取<?php echo $config['name']; ?>推送token">
                                [获取token]
                            </a>
                        </label>
                        <input type="text" 
                               id="<?php echo $engine_type; ?>_token" 
                               name="<?php echo $engine_type; ?>_token" 
                               value="<?php echo esc_attr($current['site_token']); ?>" 
                               placeholder="<?php echo $config['placeholder']; ?>" 
                               class="regular-text">
                        <p class="description"><?php echo $config['help_text']; ?></p>
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        
        <div class="submit-section">
            <?php submit_button('保存配置', 'primary', 'submit', false); ?>
        </div>
    </form>
</div>

<style>
.engines-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
    gap: 20px;
    margin: 20px 0;
}

.engine-card {
    background: #fff;
    border: 1px solid #ccd0d4;
    border-radius: 8px;
    padding: 20px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.engine-header {
    display: flex;
    align-items: center;
    margin-bottom: 15px;
    padding-bottom: 10px;
    border-bottom: 1px solid #eee;
}

.engine-icon {
    font-size: 24px;
    margin-right: 10px;
}

.engine-header h3 {
    margin: 0;
    flex: 1;
}



.engine-description {
    color: #666;
    margin-bottom: 15px;
}

.form-field {
    margin-bottom: 15px;
}

.form-field label {
    display: block;
    margin-bottom: 5px;
    font-weight: 600;
}

.form-field input[type="text"] {
    width: 100%;
    max-width: 400px;
}

.token-link {
    color: #0073aa;
    text-decoration: none;
    margin-left: 5px;
}

.token-link:hover {
    text-decoration: underline;
}

.submit-section {
    margin-top: 30px;
    padding: 20px;
    background: #f9f9f9;
    border-radius: 8px;
}

.test-all-engines {
    margin-left: 10px;
}
</style>

<script>
jQuery(document).ready(function($) {
    $('.test-all-engines').on('click', function() {
        var button = $(this);
        button.prop('disabled', true).text('测试中...');
        
        $.ajax({
            url: ajaxurl,
            type: 'POST',
            data: {
                action: 'test_all_engines',
                nonce: '<?php echo wp_create_nonce("test_engines_nonce"); ?>'
            },
            success: function(response) {
                if (response.success) {
                    alert('连接测试完成！\n' + response.data.message);
                } else {
                    alert('测试失败：' + response.data.message);
                }
            },
            complete: function() {
                button.prop('disabled', false).text('测试所有连接');
            }
        });
    });
});
</script>

<style>
/* 付费功能限制样式 */
.premium-restriction {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 15px 20px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border-radius: 8px;
    font-size: 16px;
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
    transition: all 0.3s ease;
    margin: 20px 0;
}

.premium-restriction:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(102, 126, 234, 0.4);
}

.premium-icon {
    font-size: 20px;
    animation: pulse 2s infinite;
}

.premium-text {
    flex: 1;
    font-weight: 600;
}

.premium-link {
    background: rgba(255, 255, 255, 0.2);
    color: white;
    padding: 8px 16px;
    border-radius: 6px;
    text-decoration: none;
    font-weight: 700;
    transition: all 0.3s ease;
    border: 2px solid rgba(255, 255, 255, 0.3);
}

.premium-link:hover {
    background: rgba(255, 255, 255, 0.3);
    color: white;
    text-decoration: none;
    transform: scale(1.05);
}

@keyframes pulse {
    0% { transform: scale(1); }
    50% { transform: scale(1.1); }
    100% { transform: scale(1); }
}

/* 禁用状态样式 */
.premium-disabled {
    opacity: 0.6;
    pointer-events: none;
    position: relative;
}

.premium-disabled::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.8);
    z-index: 1;
    border-radius: 8px;
}
</style>